package com.onpositive.wiki3.db.catmodel.matches;

import com.carrotsearch.hppc.IntOpenHashSet;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.ICategoryMatcher;

public class SelfLinksRemover implements ICategoryMatcher {

	private int LEVEL =4;

	static int count = 0;

	@Override
	public int accept(Category st) {
		Category[] ps = st.getParentsCategories();
		for (Category z : ps) {
			if (z.hasParent(st, LEVEL)) {
				count++;
				//System.out.println(z + "->" + st + ":c" + (count++));
				z.unregisterChild(st);

			}
			Category mm = z;
			IntOpenHashSet dd=new IntOpenHashSet();
			while (mm.parentCount() == 1) {
				Category parent = mm.getParent(0);
				
				if (parent.equals(st)||dd.contains(parent.id())) {
					count++;
					//System.out.println(z + "->" + st + ":c" + (count++));
					parent.unregisterChild(mm);
					break;
				}
				dd.add(parent.id());
				mm=parent;
			}
		}
		return 0;
	}

	public static int getTotalRemovals(){
		return count;
	}

	public static void recordRemoval(Category st) {
		count++;
	}
}
